package com.aplink.generic.xmlparser;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.pojo.controlspec.GallerySpec;

public class GalleryPaser {

    public static final String CTRL_RESULT = "ctrlResult";
    public static final String DEFAULT_IMAGE = "defaultImage";
    public static final String DELETE_PICTURE = "deletePhoto";
    public static final String IMAGE_HEIGHT = "imageHeight";
    public static final String IMAGE_PATH = "imagePath";
    public static final String IMAGE_WIDTH = "imageWidth";
    public static final String LOAD_ALL = "loadAll";
    public static final String MAX_ZOOM = "maxZoom";
    public static final String MIN_ZOOM = "minZoom";
    public static final String NO_PHOTO = "noPhoto";
    public static final String SCAN_BARCODE = "scanBarcode";
    public static final String SELECT_FROM_GALLERY = "photoFromGallery";
    public static final String TAKE_PICTURE = "capturePhoto";

    public static GallerySpec parse(Node nodeFormat) {
        final GallerySpec gallerySpec = new GallerySpec();
        if (nodeFormat.hasAttributes()) {
            String value = XMLPaserUtil.getAttrValue(nodeFormat,
                    GalleryPaser.IMAGE_WIDTH);
            if (!TextUtils.isEmpty(value)) {
                gallerySpec.setImageWidth(Integer.parseInt(value));
            }

            value = XMLPaserUtil.getAttrValue(nodeFormat,
                    GalleryPaser.IMAGE_HEIGHT);
            if (!TextUtils.isEmpty(value)) {
                gallerySpec.setImageHeight(Integer.parseInt(value));
            }

            value = XMLPaserUtil.getAttrValue(nodeFormat,
                    GalleryPaser.DEFAULT_IMAGE);
            if (!TextUtils.isEmpty(value)) {
                gallerySpec.setDefaultImage(value);
            }

            value = XMLPaserUtil
                    .getAttrValue(nodeFormat, GalleryPaser.MIN_ZOOM);
            if (!TextUtils.isEmpty(value)) {
                gallerySpec.setMinZoom(Float.parseFloat(value));
            }

            value = XMLPaserUtil
                    .getAttrValue(nodeFormat, GalleryPaser.MAX_ZOOM);
            if (!TextUtils.isEmpty(value)) {
                gallerySpec.setMaxZoom(Float.parseFloat(value));
            }

            value = XMLPaserUtil
                    .getAttrValue(nodeFormat, GalleryPaser.NO_PHOTO);
            if (!TextUtils.isEmpty(value)) {
                gallerySpec.setNoPhoto(value);
            }

            value = XMLPaserUtil
                    .getAttrValue(nodeFormat, GalleryPaser.LOAD_ALL);
            if (!TextUtils.isEmpty(value)) {
                if (value.equalsIgnoreCase("N")) {
                    gallerySpec.setLoadAll(false);
                }
            }
        }
        final Element elementFormat = (Element) nodeFormat;
        NodeList nodeList = elementFormat
                .getElementsByTagName(GalleryPaser.TAKE_PICTURE);
        if (nodeList.getLength() == 1) {
            final String takePictureID = XMLPaserUtil.getNodeValue(nodeList
                    .item(0));
            gallerySpec.setTakePictureID(takePictureID);
        }

        nodeList = elementFormat
                .getElementsByTagName(GalleryPaser.DELETE_PICTURE);
        if (nodeList.getLength() == 1) {
            final String deleteID = XMLPaserUtil.getNodeValue(nodeList.item(0));
            gallerySpec.setDeletePictureID(deleteID);
        }

        nodeList = elementFormat
                .getElementsByTagName(GalleryPaser.SELECT_FROM_GALLERY);
        if (nodeList.getLength() == 1) {
            final String selectFromGallery = XMLPaserUtil.getNodeValue(nodeList
                    .item(0));
            gallerySpec.setSelectFromGalleryID(selectFromGallery);
        }

        nodeList = elementFormat
                .getElementsByTagName(GalleryPaser.SCAN_BARCODE);
        if (nodeList.getLength() == 1) {
            nodeFormat = nodeList.item(0);
            if (nodeFormat.hasAttributes()) {
                final String controlResults = XMLPaserUtil.getAttrValue(
                        nodeFormat, GalleryPaser.CTRL_RESULT);
                gallerySpec.setBarcodeResultsID(controlResults);
            }
            final String scanBarcode = XMLPaserUtil.getNodeValue(nodeFormat);
            gallerySpec.setScanBarcode(scanBarcode);
        }
        return gallerySpec;
    }
}
